from PIL import Image
import os
from PIL import Image, ImageChops

# 设置根目录和目标文件夹
root_dir = 'D:/DeepModel/le_jpg/exing'
target_dir = 'D:/DeepModel/le_MultJpg/exing'
# root_dir = 'D:/DeepModel/data_lexing/train/viciousData'
# target_dir = 'D:/DeepModel/Mult_data_lexing/train/viciousData'
# root_dir = 'D:/DeepModel/data_lexing/test/benignData'
# target_dir = 'D:/DeepModel/Mult_data_lexing/test/benignData'
# root_dir = 'D:/DeepModel/data_lexing/test/viciousData'
# target_dir = 'D:/DeepModel/Mult_data_lexing/test/viciousData'

# 确保目标文件夹存在
if not os.path.exists(target_dir):
    os.makedirs(target_dir)

# 获取根目录下所有.jpg文件
image_files = [f for f in os.listdir(root_dir) if f.endswith('.jpg')]

# print(image_files)

# 遍历所有图像文件
for img_file in image_files:
    if 'MA' in img_file:
        # 获取对应的NI图像文件名
        ni_img_file = img_file.replace('MA', 'NI')

        # 检查NI图像文件是否存在
        if ni_img_file in image_files:
            # 打开两个图像文件
            wa_img = Image.open(os.path.join(root_dir, img_file))
            ni_img = Image.open(os.path.join(root_dir, ni_img_file))

            # 对两个图像进行相乘操作
            multiplied_img = ImageChops.multiply(wa_img, ni_img)

            # 保存相乘后的图像到目标文件夹
            multiplied_img.save(os.path.join(target_dir, f'multiplied_{img_file}'))